package com.twohand.admin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twohand.admin.Dto.OrderCountDto;
import com.twohand.admin.Dto.SalesListQueryDto;
import com.twohand.admin.Entity.Salesman;
import com.twohand.admin.Entity.User;
import com.twohand.admin.Mapper.SalesmanMapper;
import com.twohand.admin.Service.SalesmanService;
import com.twohand.admin.Service.UserService;
import com.twohand.admin.Util.JwtUtil;
import com.twohand.admin.Util.PageResponse;
import com.twohand.admin.Vo.OrderCountVo;
import com.twohand.admin.Vo.SelectOptions;
import com.twohand.utils.Response.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SalesmanServiceImpl implements SalesmanService {
    @Autowired
    SalesmanMapper salesmanMapper;
    @Autowired
    UserService userService;
    @Override
    public PageResponse<Salesman> salesList(SalesListQueryDto salesListQueryDto) {
        PageHelper.startPage(salesListQueryDto.getPageNum(),salesListQueryDto.getPageSize());
        QueryWrapper<Salesman> queryWrapper = new QueryWrapper<>();
        String name = salesListQueryDto.getName();
        if(name != null && !name.isEmpty() && !name.trim().equals("")){
            queryWrapper.like("name",salesListQueryDto.getName());
        }
        queryWrapper.orderByDesc("is_default");
        List <Salesman> salesmanList = salesmanMapper.selectList(queryWrapper);
        PageInfo<Salesman> swipePageInfo = new PageInfo<>(salesmanList);
        return new PageResponse<Salesman>(swipePageInfo,salesmanList);
    }

    @Override
    public Boolean addSales(Salesman salesman) {
        return salesmanMapper.insert(salesman) > 0;
    }

    @Override
    public Boolean changeSales(Salesman salesman) {
        return salesmanMapper.updateById(salesman) > 0;
    }

    @Override
    public Boolean delSales(Integer id) {
        return salesmanMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean setDefault(Integer id) {
        salesmanMapper.allNoDefault();
        Salesman salesman = salesmanMapper.selectById(id);
        salesman.setIsDefault(1);
        return salesmanMapper.updateById(salesman) > 0;
    }

    @Override
    public List<SelectOptions> getSalesOptions() {
        return salesmanMapper.getSalesOptions();
    }

    @Override
    public Salesman getDefaultSales() {
        QueryWrapper<Salesman> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_default",1);
        Salesman salesman = salesmanMapper.selectOne(queryWrapper);
        if(salesman == null){
            throw new BaseException(500,"未设置默认销售，请先设置");
        }
        return salesman;
    }

    @Override
    public PageResponse<OrderCountVo> getOrderCountBySale(OrderCountDto orderCountDto) {
        User user = JwtUtil.getCurrentUser();
        User user1 = userService.getUserById(user.getId());
        Integer sid = null;
        if(user1.getSid()!=0){
            sid = user1.getSid();
        }
        PageHelper.startPage(orderCountDto.getPageNum(),orderCountDto.getPageSize());
        QueryWrapper<Salesman> queryWrapper = new QueryWrapper<>();
        List <OrderCountVo> orderCountVoList = salesmanMapper.getOrderCount(orderCountDto.getStartTime(),orderCountDto.getEndTime(),sid);
        PageInfo<OrderCountVo> swipePageInfo = new PageInfo<>(orderCountVoList);
        return new PageResponse<OrderCountVo>(swipePageInfo,orderCountVoList);
    }
}
